package at.codebase.web.servlets.api;

import org.json.simple.JSONObject;

import at.codebase.db.dao.EApiAccessDao;
import at.codebase.db.dao.EMessageExtDao;
import at.codebase.db.dao.EUserDao;
import at.codebase.db.entities.EApiPartner;
import at.codebase.db.entities.EMessageExt;
import at.codebase.db.entities.EMessageExt.UserExt;
import at.codebase.db.entities.EUser;
import at.codebase.security.SecurityUtils;
import at.codebase.web.util.LocalRequest.RequestInformation;
import at.codebase.web.util.URLParameters;
import at.codebase.web.util.URLParameters.ParameterDoesNotExistException;
import at.codebase.web.util.URLParameters.ParameterException;
import at.codebase.web.util.servlet.ApiSecureServlet;

public class SendMessageApiServlet extends ApiSecureServlet {

	@SuppressWarnings("unchecked")
	@Override
	protected JSONObject processRequest(RequestInformation requestInfo, String authToken) {
		EApiPartner partner = EApiAccessDao.getPartner(authToken);
		if(partner != null){
		
			URLParameters parameters = requestInfo.getParameters();
			
			try {
				
				
				String source_user_id = parameters.getString("source_user_id");
				String source_user_name = parameters.getString("source_user_name");
				long destination_user_id = parameters.getLong("destination_user_id");
				String message = parameters.getString("message");
				
				String subject = "";
				try {
					subject = parameters.getString("subject");
				} catch(ParameterDoesNotExistException ex) {
				}
	
				String source_conversation_id = parameters.getString("source_conversation_id");
				
				//Optional
				//String subject = parameters.getString("subject");
				String item_id = null;
				try {
					item_id = parameters.getString("item_id");
				} catch (ParameterException e){ }
	
				if(source_user_id != null && !source_user_id.trim().equals("")
					&& source_user_name != null && !source_user_name.trim().equals("")
					&& message != null && !message.trim().equals("")
					&& source_conversation_id != null && !source_conversation_id.trim().equals("")){
					
					if(!subject.isEmpty()) {
						message = subject + ":\n" + message;
					}
					
					EUser user = EUserDao.getUserById(new Long(destination_user_id));
					UserExt userAddressorExt = new UserExt(EApiPartner.ref(partner.getPartnerId()), source_user_id, source_user_name);
					
					
					if(user != null){
						//if(item_id == null || item_id.trim().equals("")){
							//private message						
							EMessageExt msg = EMessageExt.createPrivateIncomingMessageExt(userAddressorExt, EUser.ref(user.getId()), message, source_conversation_id);
							EMessageExtDao.addMessage(msg);
							
							JSONObject result = successResult();
							result.put("conversation_id", new Long(0));
							return result;
						/*}else{
							EMessageExt msg = EMessageExt.createItemIncomingMessageExt(userAddressorExt, EUser.ref(user.getId()), item_id, message, source_conversation_id);
							EMessageExtDao.addMessage(msg);
				
							JSONObject result = successResult();
							result.put("conversation_id", new Long(0));
							return result;
						}	*/
					}else{
						return badRequestResult("User id="+destination_user_id+" does not exists");
					}
					
					
				}
		
				return badRequestResult("Bad search query");
				
			} catch (ParameterException e) {
				return badRequestResult("Parameters invalid");
			}
		
		} else {
			return badRequestResult("Valid auth_token but you are not an partner");
		}
	}

}
